requirejs.config({
    paths: {
        artplayer: "/assets/addons/health/libs/artplayer.min"
    }
})
define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'artplayer'], function ($, undefined, Backend, Table, Form) {
    var Controller = {
        index     : function () {

            new Vue({
                el: "#containerBox",
                data() {
                    return {
                        chooseType    : '1',
                        data          : [],
                        offset        : 0,
                        limit         : 10,
                        totalPage     : 0,
                        currentPage   : 1,
                        searchForm    : {name: ''},
                        searchFormInit: {name: ''}
                    }
                },
                created() {
                    this.getData()
                },
                methods: {
                    getData() {
                        let that = this;
                        let searchForm = that.searchForm;
                        searchForm.page = that.currentPage;
                        searchForm.limit = that.limit;
                        Fast.api.ajax({
                            url    : 'health/website/video/index',
                            loading: true,
                            type   : 'GET',
                            data   : searchForm,
                        }, function (ret, res) {
                            that.data = res.data.rows;
                            that.totalPage = res.data.total;
                            return false;
                        })
                    },
                    operation(type, id) {
                        let that = this;
                        switch (type) {
                            case 'preview':
                                Fast.api.open("health/website/video/preview?id=" + id.id, "预览" + id.name);
                                break;
                            case 'edit':
                                Fast.api.open('health/goods/fulu_goods/edit?id=' + id + "&type=edit", '编辑福禄商品', {
                                    callback() {
                                        that.getData();
                                    }
                                })
                                break;
                            case 'del':
                                that.$confirm('此操作将删除该商品, 是否继续?', '提示', {
                                    confirmButtonText: '确定',
                                    cancelButtonText : '取消',
                                    type             : 'warning'
                                }).then(() => {
                                    Fast.api.ajax({
                                        url    : 'health/goods/fulu_goods/del/ids/' + id,
                                        loading: false,
                                        type   : 'POST',
                                    }, function (ret, res) {
                                        that.getData()
                                    })
                                }).catch(() => {
                                });

                                break;
                            case 'filter':
                                this.getData()
                                break;
                            case 'clear':
                                this.searchForm = JSON.parse(JSON.stringify(that.searchFormInit));
                                break;
                        }
                    },
                    handleSizeChange(val) {
                        this.offset = 0
                        this.limit = val;
                        this.currentPage = 1;
                        this.getData()
                    },
                    handleCurrentChange(val) {
                        this.currentPage = val;
                        this.offset = (val - 1) * this.limit;
                        this.getData()
                    },
                    tableRowClassName({rowIndex}) {
                        if (rowIndex % 2 === 1) {
                            return 'bg-color';
                        }
                        return '';
                    },
                    tableCellClassName({columnIndex}) {
                        if (columnIndex === 2 || columnIndex === 9) {
                            return 'cell-left';
                        }
                        return '';
                    },
                }
            })
        },
        preview   : function () {
            let payer = new Artplayer({
                url      : Config.video.url || '',
                container: '.data-play-video',
                controls : [
                    {html: '全屏播放', position: 'right', click: () => payer.fullscreen = !payer.fullscreen},
                ]
            })
        },
        edit      : function () {
            Controller.detailInit('edit');
        },
        detailInit: function (type) {
            function urlParams(par) {
                let value = '';
                window.location.search.replace("?", '').split("&").forEach(i => {
                    if (i.split('=')[0] == par) {
                        value = JSON.parse(decodeURI(i.split('=')[1]))
                    }
                })
                return value
            }

            var detailInit = new Vue({
                el: "#detailInit",
                data() {
                    return {
                        optType       : type,
                        id            : urlParams('id'),
                        detailForm    : {},
                        detailFormInit: {
                            name        : '',
                            icon        : '',
                            goods_code  : '',
                            cost_price  : '',
                            price       : '',
                            vip_price   : '',
                            status      : 1,
                            remark      : '',
                            must_element: 1
                        },
                        typeMap       : [],
                        rules         : {
                            name        : [{
                                required: true,
                                message : '请输入轮播图名称',
                                trigger : 'blur'
                            }],
                            goods_code  : [{
                                required: true,
                                message : '请输入福禄商品编号',
                                trigger : 'blur'
                            }],
                            icon        : [{
                                required: true,
                                message : '请上传商品图标',
                                trigger : 'blur'
                            }],
                            cost_price  : [{
                                required: true,
                                message : '请输入商品采购价',
                                trigger : 'blur'
                            }],
                            price       : [
                                {
                                    required: true,
                                    message : '请输入商品售价',
                                    trigger : 'blur'
                                },
                                {
                                    message  : '售价不能小于等于采购价',
                                    validator: this.priceValidate,
                                    trigger  : 'blur'
                                }
                            ],
                            vip_price   : [
                                {
                                    required: true,
                                    message : '请输入商品会员价',
                                    trigger : 'blur'
                                },
                                {
                                    validator: this.priceValidate,
                                    trigger  : 'blur'
                                }
                            ],
                            must_element: [{
                                required: true,
                                message : '请选择下单必要素',
                                trigger : 'blur'
                            }],
                        }

                    }
                },
                created() {
                    this.typeMap = Config.type_map
                    this.detailForm = JSON.parse(JSON.stringify(this.detailFormInit));
                    if (this.optType === 'edit') {
                        for (key in this.detailForm) {
                            this.detailForm[key] = Config.row[key]
                        }
                    } else {
                        if (Config.position) {
                            this.detailForm.position = Config.position;
                        }
                    }
                    console.log(this.id)
                },
                methods: {
                    priceValidate(rule, value, callback) {
                        let costPrice = Number(this.detailForm.cost_price);
                        let currentPrice = Number(value)
                        let message = rule.message ? rule.message : '不能小于等于采购价';
                        if (currentPrice > costPrice) {
                            callback();
                        } else {
                            callback(new Error(message));
                        }
                    },
                    addImg(type, index, multiple) {
                        let that = this;
                        parent.Fast.api.open("general/attachment/select?multiple=" + multiple, "选择图片", {
                            callback: function (data) {
                                that.detailForm.icon = data.url;
                            }
                        });
                        return false;
                    },
                    delImg(type, index) {
                        this.detailForm.icon = ''
                    },
                    submitForm(type, formName) {
                        let that = this;
                        if (type) {
                            this.$refs[formName].validate((valid) => {
                                if (valid) {
                                    let formData = JSON.stringify(that.detailForm);
                                    if (that.optType === 'edit') {
                                        Fast.api.ajax({
                                            url    : 'health/goods/fulu_goods/edit/id/' + that.id,
                                            loading: true,
                                            data   : {
                                                data: formData
                                            }
                                        }, function (ret, res) {
                                            Fast.api.close({data: true});
                                        })
                                    } else {
                                        Fast.api.ajax({
                                            url    : 'health/goods/fulu_goods/add',
                                            loading: true,
                                            data   : {
                                                data: formData
                                            }
                                        }, function (ret, res) {
                                            Fast.api.close({data: true});
                                        })
                                    }
                                } else {
                                    return false;
                                }
                            });
                        } else {
                            Fast.api.close({data: false});
                        }
                    },
                },
            })
        },
    };

    return Controller;
});